"""
爬虫思路
（一）数据来源分析
    1、爬什么？（需求分析）
        视频、标题、评论

    2、去哪儿爬？（接口分析）
        - 找URL
        - 触发的接口怎么分析：可以先清空数据包，单独触发接口获取API
        - 视频一般都是ajax请求（动态数据）。
        - 怎么去识别我们要的数据是静态数据还是动态数据呢？
            源代码里面能搜索到  == 静态
            反之 == 动态

（二）爬虫代码实现
    1、发送请求：构造请求头和请求体
    2、数据获取
    3、解析数据
    4、保存数据
"""
import httpx


def click_like(url, headers, json_data):
    # 介绍怎么使用httpx库
    with httpx.Client() as client:
        resp = client.post(url=url, headers=headers, json=json_data)
        if resp.json()['data']['visionVideoLike']['result'] == 1:
            print('点击爱心！')


def main():
    url = 'https://www.kuaishou.com/graphql'
    cancel = int(input('点赞输入0，取消赞输入1 \n>>>'))
    # 爬虫的本质是伪装成人来请求
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
        'cookie': 'kpf=PC_WEB; clientid=3; did=web_717ad7a5d13c8afb38e63e8aacaf6e7f; clientid=3; userId=3498836671; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABM8XeVX0hQDdc3BOcmPv9VAfjGjQuvVnA_Ky2Qtq_Hnbm3EckQSBTDlSdEF0qiZxppNHik-BhRVqGCIp77r4VitLUgzMkQteX_SXx_OEAwnnxCQg6P-O5V8CD2KE9Vr-daJCCcc4PUHWmVFd-x-EW1aVgZKQkAduygCBnbAFRPsolOBc9pdicKR_k5tS-Zr3RdtxcKAwUXx4GpFDx093T2RoS-1Rj5-IBBNoxoIePYcxZFs4oIiDXYVyiynis_jdKl8XtnHrxkhQERxeytokC7dw78xfd8ygFMAE; kuaishou.server.web_ph=58d3948d0d6d17dcc58cfd2197618a9d8cf4; kpn=KUAISHOU_VISION',
    }
    json_data = {
        "operationName": "visionVideoLike",
        "variables": {
            "photoId": "3x4rjuryq6b2dnc",    # 短视频id
            "photoAuthorId": "3xbxsdftqg7apd9",  # 作者id
            "cancel": cancel,
            "expTag": "1_a/2002189400393813170_xpcwebbrilliantxxcarefully0"
        },
        "query": "mutation visionVideoLike($photoId: String, $photoAuthorId: String, $cancel: Int, $expTag: String) {\n  visionVideoLike(photoId: $photoId, photoAuthorId: $photoAuthorId, cancel: $cancel, expTag: $expTag) {\n    result\n    __typename\n  }\n}\n"
    }
    click_like(url, headers, json_data)

if __name__ == '__main__':
    main()
